//
//  TT_WebView.m
//  BaseProject
//
//  Created by 王洋 on 2021/1/25.
//  Copyright © 2021 王洋. All rights reserved.
//

#import "TT_WebView.h"

@interface TT_WebView ()

@property (weak, nonatomic) IBOutlet UIButton *closeBtn;
@property (weak, nonatomic) IBOutlet UIView *bgView;

@end

@implementation TT_WebView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self.bgView addSubview:self.webview];
    [self.webview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
}

- (void)loadWithUrl:(NSString *)url htmlStr:(NSString *)htmlStr {
    
    self.webUrl = url;
    self.htmlString = htmlStr;
    
    if (self.webUrl.length > 0) {
        
        NSString *webUrl = self.webUrl;
        self.webUrl = [webUrl stringByReplacingOccurrencesOfString:@" " withString:@""];

        if ([self.webUrl hasPrefix:@"http"]) {
            [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.webUrl]]];
        } else {
            NSString *url = [NSString stringWithFormat:@"http://%@",self.webUrl];
            [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
        }
    }
    
    if (self.htmlString.length > 0) {
        [self.webview loadHTMLString:self.htmlString baseURL:nil];
    }
}

- (IBAction)closeAction:(id)sender {
    if (self.TT_WebViewCloseBlock) {
        self.TT_WebViewCloseBlock();
    }
}


#pragma mark VR网页初始化
- (WKWebView *)webview {
    if (!_webview) {
        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
        WKUserContentController *userController = [[WKUserContentController alloc] init];
        configuration.userContentController = userController;
        _webview = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
        _webview.backgroundColor = UIColorHex(0xf1f1f1);
    }
    return _webview;
}

@end
